local chongxu = fk.CreateSkill {
  name = "ym_water__chongxu",
}

Fk:loadTranslationTable {
  ["ym_water__chongxu"] = "冲虚",
  [":ym_water__chongxu"] = "出牌阶段限一次，你可以进行集灵来获得分数，然后你可以消耗分数执行效果：升级〖妙剑〗（3分）；升级〖莲华〗（3分）；摸一张牌（2分）。",

  ["#ym_water__chongxu"] = "你可以发动 冲虚，进行1次飞升，以升级“妙剑”或“莲华”",
  ["#ym_water__chongxu-choice"] = "冲虚：消耗积分执行效果（还剩%arg分）",
  ["update_miaojian"] = "升级〖妙剑〗（3分）",
  ["update_lianhuas"] = "升级〖莲华〗（3分）",
  ["ym_water__chongxu_draw"] = "摸一张牌（2分）",

  ["$ym_water__chongxu1"] = "阳炁冲三关，斩尸除阴魔。",
  ["$ym_water__chongxu2"] = "蒲团清静坐，神归了道真。",
}
Fk:addMiniGame{
  name = "ym_water__chongxu",
  qml_path = "packages/water_wegame/qml/chongxu",
  default_choice = function(player, data)
    return "0"
  end,
  update_func = function(player, data)
    local room = player.room
    if #data == 1 and data[1] == "confirm" then
        for _, p in ipairs(room:getOtherPlayers(player, false)) do
            p:doNotify("UpdateMiniGame", "")
        end
    else
        local dat = table.concat(data, ",")
        local success, syncData = pcall(json.decode, string.sub(dat, string.find(dat, ",", 1, true) + 1))
        if success and syncData.type then
          if syncData.type == "score-change" then
            if syncData.subtype == "addScore" then
              player.room:broadcastPlaySound("./packages/water_wegame/image/anim/chongxu/addScore")
            else
              player.room:broadcastPlaySound("./packages/water_wegame/image/anim/chongxu/reduceScore")
            end
          elseif syncData.type == "game-end" then
              player.client_reply = tostring(syncData.score)
              player.room:broadcastPlaySound("./packages/water_wegame/image/anim/chongxu/Five_lotus")
          end
        end
        for _, p in ipairs(room:getOtherPlayers(player, false)) do
            p:doNotify("UpdateMiniGame", dat)
        end
    end
  end,
}
chongxu:addEffect("active", {
  anim_type = "drawcard",
  card_num = 0,
  target_num = 0,
  prompt = "#ym_water__chongxu",
  can_use = function(self, player)
    return player:usedSkillTimes(chongxu.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local data_table = {}
    for _, p in ipairs(room.players) do
      data_table[p.id] = {
        p == player,
        p ~= player and p:isAlive(),
      }
    end
    room:askToMiniGame(room.players, {
      skill_name = chongxu.name,
      game_type = "ym_water__chongxu",
      data_table = data_table
    })
    local n = tonumber(player.client_reply) and tonumber(player.client_reply) or 0
    if n > 0 then
      local choice = "ym_water__chongxu_draw"
      local all_choices = {"update_miaojian", "update_lianhuas", "ym_water__chongxu_draw", "Cancel"}
      while n > 1 and choice ~= "Cancel" and not player.dead do
        local choices = table.simpleClone(all_choices)
        if not (n > 2 and player:hasSkill("ym_water__lianhua", true) and player:getMark("ym_water__lianhua") < 2) then
          table.remove(choices, 2)
        end
        if not (n > 2 and player:hasSkill("ym_water__miaojian", true) and player:getMark("ym_water__miaojian") < 2) then
          table.remove(choices, 1)
        end
        choice = room:askToChoice(player, {
          choices = choices,
          skill_name = chongxu.name,
          prompt = "#ym_water__chongxu-choice:::"..n,
          all_choices = all_choices,
        })
        room:broadcastPlaySound("./packages/water_wegame/image/anim/chongxu/Select_rewards")
        if choice == "ym_water__chongxu_draw" then
          n = n - 2
          player:drawCards(1, chongxu.name)
        elseif choice == "update_miaojian" then
          n = n - 3
          room:addPlayerMark(player, "ym_water__miaojian", 1)
        elseif choice == "update_lianhuas" then
          n = n - 3
          room:addPlayerMark(player, "ym_water__lianhua", 1)
        end
      end
    end
  end,
})

return chongxu
